﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SyntaxHighlight;

namespace JsEditor
{
    public class JavaScriptTokenParser : ISyntaxLexer
    {
        public Object Parent { get; set; }
        private JsLexxer ts = null;
        public override void Parse(string text, int caret_position)
        {

            _tokens.Clear();
            this.ts = new JsLexxer(text, 1);
            TokenLex t = 0;

            while ((t = ts.nextToken()) != TokenLex.EOF)//
            {
                switch (t)
                {
                    case TokenLex.KeyWord:
                        //CodeTokenType type = CodeTokenType.Keyword;
                        CodeToken ct = new CodeToken();
                        ct.TokenType = CodeTokenType.Keyword;
                        ct.Start = ts.Start();
                        ct.End = ts.End();
                        _tokens.Add(ct);
                        break;
                    case TokenLex.Comment:
                        //CodeTokenType type = CodeTokenType.Keyword;
                        CodeToken ct1 = new CodeToken();
                        ct1.TokenType = CodeTokenType.Comment;
                        ct1.Start = ts.Start();
                        ct1.End = ts.End();
                        _tokens.Add(ct1);
                        break;
                    case TokenLex.CommentMult:
                        //CodeTokenType type = CodeTokenType.Keyword;
                        CodeToken ct2 = new CodeToken();
                        ct2.TokenType = CodeTokenType.Comment;
                        ct2.Start = ts.Start();
                        ct2.End = ts.End();
                        _tokens.Add(ct2);
                        break;
                    case TokenLex.String:
                        //CodeTokenType type = CodeTokenType.Keyword;
                        CodeToken ct3 = new CodeToken();
                        ct3.TokenType = CodeTokenType.String;
                        ct3.Start = ts.Start();
                        ct3.End = ts.End();
                        _tokens.Add(ct3);
                        break;
                }

            }
            if (EditEvent != null)
            {
                EditEvent(Parent, new IsEditArgs());
            }

        }

        

       
        public override System.Windows.Input.Key SuggestionListTriggerKey
        {
            get
            {
                return System.Windows.Input.Key.OemPeriod;
            }
        }

        public override bool CanShowSuggestionList(int caret_position)
        {
            return true;
        }
        public event EditEventDelegate EditEvent;
    }// end for class

    public delegate void EditEventDelegate(object sender, IsEditArgs args);

    public class IsEditArgs : EventArgs
    {
        public String Title { get; set; }
    }

}
